package com.zjdiepu.www.util;

import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.net.URL;

import org.apache.commons.lang3.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.rtf.RtfWriter2;

/**
 * 生成word文档，包含身份证正反面和授权书照片
 * 
 * @author Administrator
 *
 */
public class WordUtil {
	public static final Logger logger = LoggerFactory.getLogger(WordUtil.class);
	
	/**
	 * 导出word <br/>
	 * @param os
	 *            输出流 <br/>
	 * @param imgUrls
	 *            图片路径 <br/>
	 *  说明：产生tempFilePath临时文件
	 */
	public static void createWord(String tempFilePath, String... imgUrls) {
		if (ArrayUtils.isEmpty(imgUrls)) {
			ExceptionHelper.thow("999", "图片路径为空,无法导出Word");
			return;
		}
		Document document = new Document(PageSize.A4);
		try(
			FileOutputStream os = new FileOutputStream(tempFilePath);
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			){
			RtfWriter2.getInstance(document, baos);
			document.open();
			for (String imgUrl : imgUrls) {
				if(imgUrl.contains(".doc?")){
					continue;
				}
				try{
					Paragraph paragraph = new Paragraph();
					URL url = new URL(imgUrl);
					Image img = Image.getInstance(url);
					float width = img.getWidth();
					float height = img.getHeight();
					float realWidth = 0.0f;
					float realHeight = 0.0f;
					if(width <= 520f && height <= 1024f) {
						realWidth = width;
						realHeight = height;
					} else if(width > 520f && height > 1024f) {
						float rateWidth = 520f / width;
						float rateHeight = 1024f / height;
						if(rateWidth >= rateHeight) {
							realWidth = width * rateHeight;
							realHeight = height* rateHeight;
						} else {
							realWidth = width * rateWidth;
							realHeight = height * rateWidth;
						}
					} else if(width > 520f) {
						float rateWidth = 520f / width;
						realWidth = width* rateWidth;
						realHeight = height * rateWidth;
					} else { //plainHeight > 1024f
						float rateHeight = 1024f / height;
						realWidth = width * rateHeight;
						realHeight = height* rateHeight;
					}
					img.scaleAbsolute(realWidth, realHeight);
					paragraph.add(img);
					document.add(paragraph);
				} catch (Exception e){
					logger.error("生成Word发生异常.{}", e);
					continue;
				}
			}
			document.close();
			baos.writeTo(os);
		} catch (Exception e) {
			logger.error("生成Word发生异常.{}", e);
		}
	}
	
	/**
	 * 下载文档
	 * @param fileName
	 * @param fileUrl
	 * @throws Exception
	 */
    public static void downloadFile(String fileName,String fileUrl) throws Exception
    {
        URL url = null;
        url = new URL(fileUrl);
        String cPath = "C://"+fileName;
        System.out.println(url.openStream());
        DataInputStream dataInputStream = new DataInputStream(url.openStream());
        FileOutputStream fileOutputStream = new FileOutputStream(new File(cPath));
        ByteArrayOutputStream output = new ByteArrayOutputStream();

        byte[] buffer = new byte[1024];
        int length;

        while ((length = dataInputStream.read(buffer)) > 0) 
        {
            output.write(buffer, 0, length);
        }
        fileOutputStream.write(output.toByteArray());
        dataInputStream.close();
        fileOutputStream.close();
    }
	
	public static void main(String[] args) throws Exception {
		downloadFile("20171218113522_郝青荷_2204.doc","http://dpfiletest.diepusoft.com/20171218121335_郝青荷_2204.doc?e=1513574016&token=4qOP-JZKNhpEZWVT-x_eEOyN0uQfuoVhOTqA_Knl:SAQ4S-WoZmP_BFUL7vxPxu_kno8=");
	}
}